﻿<#
.SYNOPSIS 
    Sets the PeopleSoft configuration parameters.
.DESCRIPTION 
    Use this command to set the PeopleSoft Application Server configuration parameters for a specific domain.
    This command can be used to revrieve parts or all of the configuration file.
.NOTES 
    File Name  : Set-PsftConfiguration.ps1 
    Author     : Scott Kearney - scott@kearney.tk 
    Requires   : PowerShell V3 
.EXAMPLE 
    Set-PsftConfiguration -PS_HOME C:\psoft\PS_HOME -PS_CFG_HOME C:\psoft\PS_CFG_HOME -ServerType AppServer -Domain DOMAIN1 -Section Startup -Name DBName -Value QEDEMO
    Sets the database name for the application server domain named DOMAIN1.
.PARAMETER PS_HOME
    The full path to the PS_HOME directory for this domain.
.PARAMETER PS_CFG_HOME
    The full path to the PS_CFG_HOME directory for this domain.
.PARAMETER ServerType
    The type of server on which to perform the action.
.PARAMETER Domain
    The name of the domain on which to perform the action.
.PARAMETER Section
    Limits the scope to only options from this section of the configuration file.
.PARAMETER Name
    Limits the scope to only options with this name in the configuration file.
.PARAMETER Value
    Sets the value in the configuration file.
#> 
function Set-PsftConfiguration
{
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
    [OutputType([int])]
    Param
    (
        # The full path to the PS_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_HOME directory for this domain.",
                   Position=0)]
        [ValidateScript({Validate_PS_HOME $_})]
        [String]
        $PS_HOME,

        # The full path to the PS_CFG_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_CFG_HOME directory for this domain.",
                   Position=1)]
        [ValidateScript({Validate_PS_CFG_HOME $_})]
        [String]
        $PS_CFG_HOME,

        # The type of server on which to perform the action.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The type of server on which to perform the action.",
                   Position=2)]
        [ValidateSet("AppServer","PrcsScheduler")]
        $ServerType,

        # The name of the domain on which to perform the action.
        # Should be a directory under the PS_CFG_HOME
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The name of the domain on which to perform the action.",
                   Position=3)]
        [ValidateScript({Validate_Domain $PS_CFG_HOME $ServerType $_})]
        [String]
        $Domain,

        # Limits the scope to only options from this section of the configuration file.
        # Should be a section name from the psappsrv.cfg file.
        # E.g. [JOLT Listener]
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "A section name from psappsrv.cfg. E.g. ""Jolt Listener""",
                   Position=4)]
        # TODO: Validate the section name from the file
        [String]
        $Section,

        # Limits the scope to only options with this name in the configuration file.
        # Should be an option name from psappsrv.cfg
        # E.g. Port=9000
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "An option name from psappsrv.cfg. E.g. ""Port""",
                   Position=5)]
        [String]
        $Name,

        # Sets the value in the configuration file.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "Sets the value in the configuration file.",
                   Position=6)]
        $Value
    )

    Begin
    {
    }
    Process
    {
        # Set up variables
        $return = $null
        $pscfg = $null

        # Determine the configuration file based on the $ServerType
        switch ($ServerType)
        {
            'AppServer' {$pscfgFilePath = "$PS_CFG_HOME\appserv\$Domain\psappsrv.cfg"}
            'PrcsScheduler' {$pscfgFilePath = "$PS_CFG_HOME\appserv\prcs\$Domain\psprcs.cfg"}
        }

        # Get the current configuration
        $pscfg = Get-CfgContent $pscfgFilePath

        # Edit the configuration value as specified
        $pscfg[$Section][$Name] = $Value

        if ($pscmdlet.ShouldProcess("$Domain", "Set-PsftConfiguration"))
        {
            # Write back the configuration file
            $pscfg | Out-CfgFile -FilePath $pscfgFilePath -Encoding ASCII -Force
        }

        return $return
    }
    End
    {
    }
}